#ifndef BYTESTREAM_H
#define BYTESTREAM_H


class ByteStream
{
private:
	struct ByteSegment
	{
		struct ByteSegment *next;
		struct ByteSegment *previous;
		unsigned int size;
	};

	struct Reader
	{
		ByteSegment *segment;
		unsigned int segmentpos;
	};

	typedef struct ByteSegment ByteSegment;

public:
	ByteStream();
	~ByteStream();

	virtual void Write(const void *data, unsigned int size);
	virtual unsigned int Read(void *data, unsigned int size);
	virtual unsigned int Peek(void *data, unsigned int size);
	void WriteRaw(const void *data, unsigned int size);
	unsigned int ReadRaw(void *data, unsigned int size);
	unsigned int PeekRaw(void *data, unsigned int size);

	void WriteToFile(const char *filename);
	void ReadFromFile(const char* filename);
	unsigned int Size() const;
	void Clear();

protected:

private:
	ByteStream(const ByteStream& rhs); // copy constructor not implemented yet

	unsigned int ReadViaReader(Reader *reader, void *data, unsigned int size, bool removesegment);
	
	void AddSegment(ByteSegment *segment);
	void RemoveFirstSegment();
	ByteSegment* NextSegment(ByteSegment *segment);

	ByteSegment *segmentlist;
	Reader peek, read;
	unsigned int streamsize;
};

#endif //BYTESTREAM_H
